//
//  UIView+SYFView.m
//  SYFCode
//
//  Created by Angel_Yan on 16/5/26.
//  Copyright © 2016年 银联科技. All rights reserved.
//

#import "UIView+SYFView.h"

static char kActionHandlerTapBlockKey;
static char kActionHandlerTapGestureKey;
static char kActionHandlerLongPressBlockKey;
static char kActionHandlerLongPressGestureKey;

@interface UIView ()

@property (nonatomic, assign,readwrite)CGFloat maxX;
@property (nonatomic, assign,readwrite)CGFloat maxY;
@end


@implementation UIView (SYFView)

- (void)setX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x {
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height {
    return  self.frame.size.height;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width {
    return  self.frame.size.width;
}

- (CGFloat)maxX
{
    return  CGRectGetMaxX(self.frame);
}

- (CGFloat)maxY
{
    return CGRectGetMaxY(self.frame);
}

- (void)setCenterX:(CGFloat)centerX
{
	CGPoint center = self.center;
	center.x = centerX;
	self.center = center;
}

- (CGFloat)centerX {
	return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
	CGPoint center = self.center;
	center.y = centerY;
	self.center = center;
}

- (CGFloat)centerY {
	return self.center.y;
}


/*
 * 添加默认的线条和圆角
 */

- (void)addDetaultBorderLineAndCornerRadius;
{
	self.backgroundColor = [UIColor whiteColor];
	self.layer.cornerRadius = 5;
	self.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
	self.layer.masksToBounds = YES;
	
}

/**
 *  系统默认线条
 */
- (void )addLineLabelWithFrame:(CGRect )frame ;
{
    UILabel *lineLab = [[UILabel alloc] initWithFrame:frame];
    lineLab.backgroundColor = KUIColorARGBString(@"#ebebeb");
    lineLab.alpha = 0.5;
    [self addSubview:lineLab];
    
}


/*
 * 添加阴影
 */

- (void)addDetaultShadow {
	[self addShadowWithColor:[UIColor blackColor] shadowOffset:CGSizeMake(0, 0) shadowRadius:5 shadowOpacity:0.8];
}


- (void) addShadowWithColor:(UIColor *)shadowColor shadowOffset:(CGSize )shadowOffset shadowRadius:(CGFloat) shadowRadius shadowOpacity:(CGFloat)shadowOpacity
{
	
	self.backgroundColor = [UIColor whiteColor];
	self.layer.shadowColor = shadowColor.CGColor;
	self.layer.shadowOffset = shadowOffset;
	self.layer.shadowRadius = shadowRadius;
	self.layer.shadowOpacity = shadowOpacity;
	self.layer.masksToBounds = NO;
}

- (void)addTapActionWithBlock:(GestureActionBlock)block
{
    UITapGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerTapGestureKey);
    if (!gesture)
    {
        gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForTapGesture:)];
        [self addGestureRecognizer:gesture];
        objc_setAssociatedObject(self, &kActionHandlerTapGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    objc_setAssociatedObject(self, &kActionHandlerTapBlockKey, block, OBJC_ASSOCIATION_COPY);
}
- (void)handleActionForTapGesture:(UITapGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateRecognized)
    {
        GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerTapBlockKey);
        if (block)
        {
            block(gesture);
        }
    }
}
- (void)addLongPressActionWithBlock:(GestureActionBlock)block
{
    UILongPressGestureRecognizer *gesture = objc_getAssociatedObject(self, &kActionHandlerLongPressGestureKey);
    if (!gesture)
    {
        gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleActionForLongPressGesture:)];
        [self addGestureRecognizer:gesture];
        objc_setAssociatedObject(self, &kActionHandlerLongPressGestureKey, gesture, OBJC_ASSOCIATION_RETAIN);
    }
    objc_setAssociatedObject(self, &kActionHandlerLongPressBlockKey, block, OBJC_ASSOCIATION_COPY);
}
- (void)handleActionForLongPressGesture:(UITapGestureRecognizer*)gesture
{
    if (gesture.state == UIGestureRecognizerStateRecognized)
    {
        GestureActionBlock block = objc_getAssociatedObject(self, &kActionHandlerLongPressBlockKey);
        if (block)
        {
            block(gesture);
        }
    }
}

@end
